package function;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import UserDatabase.UserDatabase;

public class MBillFrame extends JFrame{
	String [] tableHead;
	String [][] user;
	JTable table;
	JLabel l_id,l_name,l_uno;
	JTextField t_id,t_name,t_uno;
	JButton b_find,b_reset,b_refresh,b_updata,b_insert;
	UserDatabase udb = new UserDatabase();
	String SQL;
	
	public MBillFrame(String SQL) {
		this.setSize(800,500);
		this.setTitle("费用管理(显示未缴费名单)");
		this.setLocation(500,150);
		this.SQL = SQL;
		init();
		this.setVisible(true);
	}
	
	public void init() {
		udb.setSQL(SQL);
		user = udb.getRecord();
		tableHead = udb.getColumnName();
		table = new JTable(user,tableHead);
		
		l_id = new JLabel("ID",JLabel.CENTER);
		l_name = new JLabel("姓名",JLabel.CENTER);
		l_uno = new JLabel("单元号",JLabel.CENTER);
		
		t_id = new JTextField();
		t_name = new JTextField();
		t_uno = new JTextField();
		
		b_find = new JButton("查询");
		b_find.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String SQL = "select user.id,name,age,user.uno,user.hno,水费,电费,燃气费,取暖费,物业费,房租 from user,bill,house where user.hno = house.hno and bill.id = user.id and type = '业主' and (水费!=0 OR 电费!=0 OR 燃气费!=0 OR 取暖费!=0 OR 物业费!=0 OR 房租!=0) and";
				String id = t_id.getText();
				String name = t_name.getText();
				String uno = t_uno.getText();
				if(id.equals("")&&name.equals("")&&uno.equals("")) {
					new MBillFrame("select user.id,name,age,user.uno,user.hno,水费,电费,燃气费,取暖费,物业费,房租 from user,bill,house where user.hno = house.hno and bill.id = user.id and type = '业主' and (水费!=0 OR 电费!=0 OR 燃气费!=0 OR 取暖费!=0 OR 物业费!=0 OR 房租!=0)");
					setVisible(false);
				}else if(!id.equals("")) {
					
					SQL = SQL+" user.id = '"+id+"' ";
					new MBillFrame(SQL);
					setVisible(false);
				
				}else if(!name.equals("")) {
				
					SQL = SQL+" name = '"+name+"' ";
					new MBillFrame(SQL);
					setVisible(false);
					
				}else if(!uno.equals("")) {
				
					SQL = SQL+" user.uno = '"+uno+"' ";
					new MBillFrame(SQL);
					setVisible(false);
					
				}
			}
			
		});
		
		b_reset = new JButton("重置");
		b_reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t_id.setText(null);
				t_name.setText(null);
			}
			
		});
		
		b_updata = new JButton("更新指定业主费用");
		b_updata.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new UpdateBillFrame();
			}
			
		});
		
		b_refresh = new JButton("刷新");
		b_refresh.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new MBillFrame("select user.id,name,age,user.uno,user.hno,水费,电费,燃气费,取暖费,物业费,房租 from user,bill,house where user.hno = house.hno and bill.id = user.id and type = '业主' and (水费!=0 OR 电费!=0 OR 燃气费!=0 OR 取暖费!=0 OR 物业费!=0 OR 房租!=0)");
				setVisible(false);
			}
			
		});
		
		b_insert = new JButton("更新所有业主费用");
		b_insert.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new UpdateAllBillFrame();
			}
			
		});
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,11,5,5));
		p.add(l_id);
		p.add(t_id);
		p.add(l_name);
		p.add(t_name);
		p.add(l_uno);
		p.add(t_uno);
		p.add(b_find);
		p.add(b_reset);
		p.add(b_refresh);
		
		JScrollPane sp = new JScrollPane(table);
		
		JPanel p_bottom = new JPanel();
		p_bottom.setLayout(new GridLayout(1,2,8,8));
		p_bottom.add(b_updata);
		p_bottom.add(b_insert);
		
		this.add(p,BorderLayout.NORTH);
		this.add(sp,BorderLayout.CENTER);
		this.add(p_bottom,BorderLayout.SOUTH);
	}
	
	
}
